package com.utils.info.util.hhy;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 爬取指定网址图片
 *
 * @author hhy
 * @date 2021/11/19 15:00:06
 * @desc 描述：爬取指定网址的图片
 */
@SuppressWarnings("ALL")
public class CrawlerImageUtil {

    /**
     * 爬取图片
     *
     * @param url   url地址
     * @return      图片地址
     */
    @SneakyThrows
    public static List<String> crawlImages(String url) {
        //创建集合
        List<String> images = new ArrayList<>();
        //链接到目标地址
        Connection connect = Jsoup.connect(url);
        //设置useragent,设置超时时间，并以get请求方式请求服务器
        Document document = connect.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).ignoreContentType(true).get();
        //线程随眠，等待响应
        Thread.sleep(1000);

        //获取图片标签
        Elements elements = document.getElementsByTag("img");
        //获取图片路径
        for (org.jsoup.nodes.Element element : elements) {
            //判断图片路径不为空，且以http开头
            if (StringUtils.isNotEmpty(element.attr("src")) && element.attr("src").startsWith("http")) {
                images.add(element.attr("src"));
            }
        }
        return images;
    }
}
